
#include "../cmd/dataprepare.h"
#include "../mainctrl.h"
#include "commmsg.h"
#include "commclass.h"
#include "CCmdDiceBonus.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_DICE_BONUS, CCmdDiceBonus);

int CCmdDiceBonus::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_WARNING(("inner cmd CCmdDiceBonus get data"));
        return Done(RET_OK);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return Done(RET_SYSTEM_FAIL);
    }

    CallBack(RET_OK);
    return RET_OK;
}

int CCmdDiceBonus::LogicDo(int iRet)
{
    return CallBack(iRet);
}

int CCmdDiceBonus::CallBack(int iRet)
{
    if (_auiClanId.Size() == 0 && _auiGid.Size() == 0)
    {
        return Done(RET_OK);
    }

    if (iRet != RET_OK)
    {
        CT_WARNING(("CCmdDiceBonus ret %d, %u", iRet, _auiGid[0]._uiGID));
        _auiGid.DelOneItem(0);
    }

    if (0 == _auiGid.Size())
    {
        int iClanIndex = -1;
        while (iClanIndex < 0)
        {
            if (0 == _auiClanId.Size())
            {
                return Done(RET_OK);
            }

            _auiClanId.DelOneItem(0);

            iClanIndex = pstMainCtrl->m_stClanMgr.GetClanIndexByClanId(_auiClanId[0]);
            if (iClanIndex >= 0)
            {
                //初始化要发送奖励的结构
                CClanEntry & stEntry = pstMainCtrl->m_stClanMgr.GetClanEntry(iClanIndex);
                CDiceRank & stDiceRank = stEntry._stDiceRank;

                //排行榜为空
                if (0 == stDiceRank._stRank.Size())
                {
                    continue;
                }
                
                for (int i = 0; i < stDiceRank._stRank.Size(); i++)
                {
                    CGidToRank stUnit;
                    stUnit._uiGID = stDiceRank._stRank[i]._uiGid;
                    stUnit._ucRank = i+1;
                    _auiGid.AddOneItem(stUnit);
                }

                //清除排行榜
                stDiceRank.Clear();
                pstMainCtrl->m_stClanMgr.UpdateToDB(stEntry._uiClanId);
                break;
            }
            else
            {
                CT_ERROR(("clan dele when dice rank bonus %d", _auiClanId[0]));
            }
        }
    }

    //防止异常，再判断一次
    if (0 == _auiGid.Size())
    {
        return CallBack(RET_OK);
    }    

    unsigned int uiGid = _auiGid[0]._uiGID;
    unsigned char ucRank = _auiGid[0]._ucRank;
    PREPARE_PLAYER_BASE_INFO(this, uiGid);
    PREPARE_PLAYER_DETAIL_INFO(this, uiGid);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGid);
    if (iIndex < 0)
    {
        CT_WARNING(("%u", uiGid));
        return CallBack(RET_SYS_ERROR);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerDetailInfoHash.HashFind(uiGid);
    if (iIndex < 0)
    {
        CT_WARNING(("%u", uiGid));
        return CallBack(RET_SYS_ERROR);
    }
    CPlayerDetailInfo& stPlayerDetailInfo = pstMainCtrl->m_stPlayerDetailInfoHash[iIndex];

    iRet = ProcOneUser(stPlayerBaseInfo, stPlayerDetailInfo, ucRank);
    if (iRet)
    {
        CT_WARNING(("%d, %d", uiGid, iRet));
    }
    
    _auiGid.ShiftDelOneItem(0);   

    PushQueue(RET_OK);
    return 0;
}


int CCmdDiceBonus::ProcOneUser (CPlayerBaseInfo& stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, unsigned char ucRank)
{
    unsigned int uiGid = stPlayerDetailInfo._uiGID;  

    //根据神树总等级,发放个人奖励.
    CDropItemData stBonus;
    int iRet = pstMainCtrl->m_stFamilyDiceMgr.GetDiceRankBonus (ucRank, stBonus);
    if (RET_OK == iRet)
    {
        pstMainCtrl->m_stItemMgr.DropBonus(OSS_MODULE_DICE, uiGid, stBonus._usItemConfigID, stBonus._iItemCount, false);

        CT_TRACE(("%d, %d, %d, %d", ucRank, uiGid, stBonus._usItemConfigID, stBonus._iItemCount));
    }
    else
    {
        CT_ERROR (("%d, %d", iRet, uiGid));
    }

    //保存数据
    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_DETAIL_INFO;
    stKey.m_u64ID = stPlayerDetailInfo._uiGID;
    pstMainCtrl->UpdateData(stKey, stPlayerDetailInfo, VOP_UPDATE);   
    

    return 0;
}
